<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("domain/ShareCouponDomain.php");
include_once("dao/CommonDAO.php");
include_once("impl/PromotionTypeImpl.php");


class ShareCouponImpl extends CommonUtils implements CommonDAO
{
	private $promoTypeImpl;
	
	public function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag 			= $DBTag;
		
		$this->promoTypeImpl 	= new PromotionTypeImpl($this->DBTag);
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id)
	{
		$sql  = 'SELECT id, userid, couponId, promoTypeId, store, code, name, descript, startDate, endDate, status, modifiedTime ';
		$sql .= "FROM share_coupon WHERE id = $id";
		
		$conn 	= null;
		$obj	= null;
		
		try {
			$conn 	= $this->getConnection($this->DBTag);
			$rs 	= mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$obj = new ShareCouponDomain();

				$obj->setId($row[0]);
				$obj->setUserId($row[1]);
				$obj->setCouponId($row[2]);
				$obj->setPromoTypeId($row[3]);
				$obj->setStore($row[4]);
				$obj->setCode($row[5]);
				$obj->setName($row[6]);
				$obj->setDescript($row[7]);
				$obj->setStartDate($row[8]);
				$obj->setEndDate($row[9]);
				$obj->setStatus($row[10]);
				$obj->setModifiedTime($row[11]);		
				
			} else {
				$this->messages = "get data failed by Id. sql : $sql <br>";
				$this->messages .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
		
		if($obj != null){
			$promoTypeObj = $this->promoTypeImpl->getById($obj->getPromoTypeId());
			
			$obj->setPromoTypeObj($promoTypeObj);
		}
		
		return $obj;
	}

	public function add($obj)
	{		
		if($obj instanceof ShareCouponDomain){			
			$sql 	 = "INSERT INTO share_coupon SET createdTime = UTC_TIMESTAMP(), descript='";
			$sql	.= addslashes($obj->getDescript());
			
			$sql 	.= "', name='";
			$sql	.= addslashes($obj->getName());
	
			$sql 	.= "', store='";
			$sql	.= addslashes($obj->getStore());
	
			$sql 	.= "', userId=";
			$sql	.= $obj->getUserId();

			$sql 	.= ", couponId=";
			$sql	.= $obj->getCouponId();
			
			$sql 	.= ", promoTypeId=";
			$sql	.= $obj->getPromoTypeId();
			
			$sql	.= ", startDate='";
			$sql	.= $obj->getStartDate();
			
			$sql	.= "', endDate='";
			$sql	.= $obj->getEndDate();

			$sql	.= "', code='";
			$sql	.= addslashes($obj->getCode());
			
			$sql	.= "', status='";
			$sql	.= $obj->getStatus();
			$sql	.= "'";
			
			$id = $this->insert($this->DBTag, $sql);
			
			$obj->setId($id);			
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of ShareCouponDomain.';
		}
		
		return $id;
	}
	
	public function edit($obj)
	{		
		if($obj instanceof ShareCouponDomain){			
			$sql 	 = "UPDATE share_coupon SET descript='";
			$sql	.= addslashes($obj->getDescript());
			
			$sql 	.= "', name='";
			$sql	.= addslashes($obj->getName());
	
			$sql 	.= "', store='";
			$sql	.= addslashes($obj->getStore());
	
			$sql 	.= "', userId=";
			$sql	.= $obj->getMerchantId();

			$sql 	.= ", couponId=";
			$sql	.= $obj->getCouponId();
			
			$sql 	.= ", promoTypeId=";
			$sql	.= $obj->getPromoTypeId();
			
			$sql	.= ", startDate='";
			$sql	.= $obj->getStartDate();
			
			$sql	.= ", endDate='";
			$sql	.= $obj->getEndDate();

			$sql	.= "', code='";
			$sql	.= addslashes($obj->getCode());
			
			$sql	.= "', status='";
			$sql	.= $obj->getStatus();
			
			$sql	.= "' WHERE id = ";
			$sql	.= $obj->id();			
			
			return $this->update($this->DBTag, $sql);			
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of ShareCouponDomain.';
		}
		
		return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM share_coupon WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function getCoupons($userId, $order = 'ORDER BY id DESC', $page = 0, $discount = 20)
	{
		return $this->getCustoms(" WHERE userId = $userId", $order, $page, $discount);
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn 		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			
			$sql 	= "SELECT count(1) FROM share_coupon $where";	
			$rs 	= mysql_query($sql, $conn);// or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if(!empty($total)){
				$sql  = 'SELECT id, userid, couponId, promoTypeId, store, code, name, descript, startDate, endDate, status, modifiedTime ';
				$sql .= " FROM share_coupon $where $order";
				$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn);
				if($rs){
					while($row = mysql_fetch_array($rs)){
						$obj = new ShareCouponDomain();

						$obj->setId($row[0]);
						$obj->setUserId($row[1]);
						$obj->setCouponId($row[2]);
						$obj->setPromoTypeId($row[3]);
						$obj->setStore($row[4]);
						$obj->setCode($row[5]);
						$obj->setName($row[6]);
						$obj->setDescript($row[7]);
						$obj->setStartDate($row[8]);
						$obj->setEndDate($row[9]);
						$obj->setStatus($row[10]);
						$obj->setModifiedTime($row[11]);
						
						$arrback[] = $obj;
					}
				}else {
					$this->message = "select failed. sql : $sql";
					$this->message .= "<br>error: " . mysql_error($conn);
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $arrback;
	}
	

	

	
	public function getMessage() 
	{
		return $this->message;
	}

	
	
}
?>
